package com.neuedu.mvc.controller;

import com.neuedu.mvc.entity.Disease;
import com.neuedu.mvc.service.DiseaseService;
import com.neuedu.mvc.util.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/disease")
public class DiseaseController extends HttpServlet {

    DiseaseService diseaseService = new DiseaseService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }


    /**
     * 列表： http://localhost:8080/mvc/disease?_type=list
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String type = request.getParameter("_type");
            if ("list".equals(type)) {
                this.list(request, response);
            } else if ("save".equals(type)) {
                this.save(request, response);
            } else if ("toEdit".equals(type)) {
                this.toEdit(request, response);
            } else if ("delete".equals(type)) {
                this.deleteById(request, response);
            }
        } catch (Exception throwables) {  //
            throwables.printStackTrace();
        }
    }


    //列表的方法
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {

        //根据条件查询
        // List<Disease> DiseaseList = diseaseService.list(request);
        Map page = diseaseService.listPage(request);

        //输出到 客户端
        JSONUtils.write(response, page);


    }


    /**
     * 保存
     * http://localhost:8080/mvc/axios_dept?_type=save&name=xxx&leader=xxx&address=xxx
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SQLException, ClassNotFoundException {

        String id = request.getParameter("id");

        boolean success = false;
        if (id != null && !"".equals(id) && !"null".equals(id)) {
            success = diseaseService.update(request);

        } else {
            success = diseaseService.save(request);
        }


        //输出到 客户端
        JSONUtils.write(response, success);

    }

    /**
     * 修改前的 查看信息
     * http://localhost:8080/mvc/axios_dept?_type=toEdit&id=xxx
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws ServletException
     */
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException, ServletException {

        Disease Disease = diseaseService.queryById(request);
        JSONUtils.write(response, Disease);
    }

    /**
     * 删除一条数据  http://127.0.0.1:8080/mvc/axios_dept?_type=delete&id=?????
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException {

        boolean success = diseaseService.removeById(request);

        JSONUtils.write(response, success);
    }


}
